package com.cse587.xmlloader;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.dom4j.DocumentException;

import com.cse587.dao.CountryDao;
import com.cse587.entities.Country;
import com.cse587.entities.CountryBorder;
import com.cse587.util.SpringFactory;

public class XmlProcessor {
	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// ////////////////////////////XML
		// Loader//////////////////////////////////
		try {
			SpringFactory sf = SpringFactory.getInstance();
			CountryDao cDao = (CountryDao) sf.getInstance()
					.getApplicationContext().getBean("countryDao");
			long startTime = System.currentTimeMillis();
			XmlLoader loader = new XmlLoader();
			List<Country> countryList = loader.loadCountries();
			 for( Country country : countryList ){
			 //country basic property
			 int id = cDao.createObject(country);
			 country.setId(id);
			 //maxLat, maxLong, minLat, minLong
			 String coodinator =
			 loader.loadCoordinates(country.getCodeISO3());
			 if( coodinator.length() >0 ){
			 String[] array = coodinator.split(",");
			 country.setMaxLat(Double.valueOf(array[0]));
			 country.setMaxLong(Double.valueOf(array[1]));
			 country.setMinLat(Double.valueOf(array[2]));
			 country.setMinLong(Double.valueOf(array[3]));
			 }
			 cDao.updateCountry(country);
			 //land area:number, year
			 String landArea = loader.loadLandArea(country.getCodeISO3());
			 if( landArea.length()>0 ){
			 String[] array1 = landArea.split(",");
			 cDao.addCountryInfo(country.getId(), "LandArea", array1[0],
			 array1[1]);
			 }
			 //population:number, year
			 String population = loader.loadPopulation(country.getCodeISO3());
			 if( population.length()>0 ){
			 String[] array2 = population.split(",");
			 cDao.addCountryInfo(country.getId(), "Population", array2[0],
			 array2[1]);
			 }
			 //GDP:number, year
			 String gdp = loader.loadGDP(country.getCodeISO3());
			 if( gdp.length()>0 ){
			 String[] array3 = gdp.split(",");
			 cDao.addCountryInfo(country.getId(), "GDP", array3[0],
			 array3[1]);
			 }
			 //HDI:number, year
			 String hdi = loader.loadHDI(country.getCodeISO3());
			 if( hdi.length()>0 ){
			 String[] array4 = hdi.split(",");
			 cDao.addCountryInfo(country.getId(), "HDI", array4[0],
			 array4[1]);
			 }
			 //border
			 String borders = loader.loadBorder(country.getCodeISO3());
			 if(borders.length()>0){
			 String[] array5 = borders.split(",");
			 List<CountryBorder> borderList = new ArrayList<CountryBorder>();
			 for (String border : array5) {
			 borderList.add(new CountryBorder(country.getCodeISO3(), border));
			 }
			 cDao.addBatchBorder(borderList);
			 }
			 }
			 //load capital
			Map<String, Country> map = loader.loadDataFromGEO();
			cDao.batchUpdateCountry(map);
			
			 //load country code
			 for(Country country : countryList){
			 System.out.println(country.getCodeISO3());
			 country.setUnCode(loader.loadUnCode(country.getCodeISO3()));
			 cDao.updateCountryUncode(country);
			 }

			List<Country> countryList2 = cDao.getALLCountry();
			for (Country country2 : countryList2) {
				if( country2.getCapitalLat() != 0 && country2.getCapitalLong() != 0 ){
					continue;
				}
				
				String result = loader.loadCapitalLatAndLong(country2
						.getCapital().replace(" ", "_"));
				if (result.contains(",")) {
					String[] pair = result.split(",");
					country2.setCapitalLat(Double.valueOf(pair[0]));
					country2.setCapitalLong(Double.valueOf(pair[1]));
					cDao.updateCapitalLatAndLong(Double.valueOf(pair[0]),
							Double.valueOf(pair[1]), country2.getCodeISO3());
				} else {
					System.out.println("fail to fetch " + country2.getCapital()
							+ "'s lat and long");
				}

			}

			long endTimw = System.currentTimeMillis();
			System.out.println("totally take:" + (endTimw - startTime) / 1000
					+ " senconds");

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
